//液压支架 特征值模板
package com.safety.algorithm.eigenvalue.hydraulicSupport;

import com.safety.algorithm.model.hydraulicSupport.HydraulicSupFrequencyModel;
import com.safety.algorithm.model.hydraulicSupport.HydraulicSupRiskDegreeModel;
import com.safety.algorithm.model.hydraulicSupport.HydraulicSupportAlgModel;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class HydraulicSupportAlgorithm {
    private double pressureAvgL = 0;
    private double pressureAvgR = 0;
    private double pressureAvgT = 0;

    //2.3.1.2 频次 ,传入6小时的数据
    public List<HydraulicSupFrequencyModel> HydraulicSupportFrequency(List<HydraulicSupportAlgModel> lists) {
        List<HydraulicSupFrequencyModel> result = new ArrayList<HydraulicSupFrequencyModel>();
        double sumL = 0;
        double sumR = 0;
        double sumT = 0;
//        double len = lists.size();
        for (HydraulicSupportAlgModel list : lists) {
            if (list.getLeftPressure() != 0) {
                sumL++;
                sumT++;
            } else if (list.getRightPressure() != 0) {//同一数据要不要加两次整架压力
                sumR++;
                sumT++;
            }
        }
        HydraulicSupFrequencyModel hydraulicSupportFrequency = new HydraulicSupFrequencyModel(new Date(), lists.get(0).getEntId(), sumL, sumR, sumT);
        result.add(hydraulicSupportFrequency);
        return result;
    }

    //2.3.1.3 闪烁危险度 半小时一次,传入近两天的数据，记下来，后面的计算使用之前的平均值
    public List<HydraulicSupRiskDegreeModel> hydraulicSupRiskDegree(List<HydraulicSupportAlgModel> lists) {
        List<HydraulicSupRiskDegreeModel> result = new ArrayList<HydraulicSupRiskDegreeModel>();
        Date checkTime = new Date();
        //48小时的均值
        double avgL = 0;
        double avgR = 0;
        double avgT = 0;
        //low为均值的十分之一
        double avgLowL = 0;
        double avgLowR = 0;
        double avgLowT = 0;
        //-1 初始值， 1 超过平均值，0不到十分之一
        double avgLFlag = -1;
        double avgRFlag = -1;
        double avgTFlag = -1;
        //前一次时间和后一次时间
        Date beforeL = null;//需要一个初始值，此处要修改
        Date beforeR = null;//需要一个初始值，此处要修改
        Date beforeT = null;//需要一个初始值，此处要修改
        Date afterL;
        Date afterR;
        Date afterT;
        //闪烁间隔危险程度I
        double i = 0;
        double len = lists.size();
        //计算均值
        for (HydraulicSupportAlgModel list : lists) {
            avgL += list.getLeftPressure();
            avgR += list.getRightPressure();
            avgT += avgL + avgR;
        }
        avgL /= len;
        avgR /= len;
        avgT /= len;
        // 使用上次存好的均值
        avgLowL = pressureAvgL / 10;
        avgLowR = pressureAvgR / 10;
        avgLowT = pressureAvgT / 10;
        for (HydraulicSupportAlgModel list : lists) {
            if (list.getLeftPressure() > avgL) {
                if (avgLFlag == 0) {
                    //判断为闪烁
                    afterL = list.getDatetime();
//                    i = 1/(afterL-beforeL);
                    HydraulicSupRiskDegreeModel hydraulicSupFrequencyModel = new HydraulicSupRiskDegreeModel(checkTime
                            , list.getEntId(), afterL, i);
                    result.add(hydraulicSupFrequencyModel);
                    beforeL = afterL;
                }
                avgLFlag = 1;
            }
            if (list.getRightPressure() > avgR) {
                if (avgRFlag == 0) {
                    //判断为闪烁
                    afterR = list.getDatetime();

                    HydraulicSupRiskDegreeModel hydraulicSupFrequencyModel = new HydraulicSupRiskDegreeModel(checkTime
                            , list.getEntId(), afterR, i);
                    result.add(hydraulicSupFrequencyModel);
                    beforeR = afterR;
                }
                avgRFlag = 1;
            }
            if (list.getLeftPressure() + list.getRightPressure() > avgT) {
                if (avgTFlag == 0) {
                    //判断为闪烁
                    afterT = list.getDatetime();
                    HydraulicSupRiskDegreeModel hydraulicSupFrequencyModel = new HydraulicSupRiskDegreeModel(checkTime
                            , list.getEntId(), afterT, i);
                    result.add(hydraulicSupFrequencyModel);
                    beforeT = afterT;
                }
                avgTFlag = 1;
            }
            if (list.getLeftPressure() < avgLowL) {
                avgLFlag = 0;
            }
            if (list.getRightPressure() < avgLowR) {
                avgRFlag = 0;
            }
            if (list.getLeftPressure() + list.getRightPressure() > avgLowT) {
                avgTFlag = 0;
            }
        }

        // 更新新的均值
        pressureAvgL = avgL;
        pressureAvgR = avgR;
        pressureAvgT = avgT;
        return result;
    }
}
